﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmTheKho : Form
    {
        private HangHoaManagement hanghoaMgt = new HangHoaManagement();
        private QuanLyKhoManagement quanlykhoMgt = new QuanLyKhoManagement();
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private CTKhoManagement ctkhoMgt = new CTKhoManagement();
        private KhoManagement khoMgt = new KhoManagement();
        private TheKho thekho = new TheKho();

        public FrmTheKho()
        {
            InitializeComponent();
        }

        public void FrmTheKho_formclosexemreport(bool isReOpen)
        {
            if (isReOpen)
                btnXem_Click(null, null);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            int iIdHangHoa = -1;
            string sMaSo = "";
            string sTenHang = "";
            
            if (dgvHangHoa.CurrentRow.Index >= 0)
            {
                iIdHangHoa = Convert.ToInt32(dgvHangHoa.CurrentRow.Cells[2].Value);
                sMaSo = dgvHangHoa.CurrentRow.Cells[0].Value.ToString();
                sTenHang = dgvHangHoa.CurrentRow.Cells[1].Value.ToString();
            }

            string sTenChiNhanh = "";
            if (cboDoiTuong.SelectedItem != null)
            {
                sTenChiNhanh = ((ChiNhanhCongTy)cboDoiTuong.SelectedItem).TenChiNhanh;
            }

            var lst = (from t in GetAllTheKho()
                       where t.NgayThang.Date >= dtpTuNgay.Value.Date && t.NgayThang.Date <= dtpDenNgay.Value.Date
                       select t);

            if (!string.IsNullOrEmpty(sMaSo))
            {
                lst = lst.Where(t => t.MaHang == sMaSo);
            }

            if (!string.IsNullOrEmpty(sTenChiNhanh))
            {
                lst = lst.Where(t => t.TenChiNhanh == sTenChiNhanh);
            }

            // Get amount of product at the begin 
            long lTonDauKy = 0;
            long lSoLuongBanDau = 0;
            if (!string.IsNullOrEmpty(sTenChiNhanh))
            {
                CTKho ctkho = (from d in GetAllCTKho()
                               where d.IdHangHoa == iIdHangHoa
                               select d).FirstOrDefault();

                if (ctkho != null)
                    lSoLuongBanDau = ctkho.SoLuongBanDau;
            }

            // Caculating amount of Ton
           var lstTon = (from t in GetAllTheKho()
                          where t.NgayThang.Date < dtpTuNgay.Value.Date
                          select t);

            if (!string.IsNullOrEmpty(sMaSo))
            {
                lstTon = lstTon.Where(t => t.MaHang == sMaSo);
            }

            if (!string.IsNullOrEmpty(sTenChiNhanh))
            {
                lstTon = lstTon.Where(t => t.TenChiNhanh == sTenChiNhanh);
            }
            
            for (int i = 0; i < lstTon.ToList().Count; i++)
            {
                long lSumNhap = 0;
                long lSumXuat = 0;

                lSumNhap += lstTon.ToList()[i].SoLuongNhap;
                lSumXuat  += lstTon.ToList()[i].SoLuongXuat;
                lTonDauKy = (lSoLuongBanDau + lSumNhap) - lSumXuat;
            }
           
                if (lst.ToList().Count > 0)
                {
                    FrmXemReport frmXemReport = new FrmXemReport(lst.ToList(), sTenHang, sMaSo, lTonDauKy);
                    frmXemReport.formclosexemreport += new FormCloseXemReportEventHandler(FrmTheKho_formclosexemreport);
                    this.Hide();
                    frmXemReport.ShowDialog();
                    this.Show();
                }
                else
                    MessageBox.Show("Không có dữ liệu phù hợp!");
        }

        private void FrmTheKho_Load(object sender, EventArgs e)
        {
            List<Kho> lst = GetAllKho();
            Kho kho = new Kho();
            kho.Id = -1;
            kho.TenKho = "Tất cả";
            lst.Add(kho);

            cboMaKho.DataSource = lst;
            cboMaKho.DisplayMember = "TenKho";
            cboMaKho.ValueMember = "Id";

            cboDoiTuong.DataSource = GetAllChiNhanhCongTy();
            cboDoiTuong.DisplayMember = "TenChiNhanh";
            cboDoiTuong.ValueMember = "Id";
            cboDoiTuong.SelectedIndex = 0;
        }

        private void cboDoiTuong_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboDoiTuong.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text != "")
                {
                    cboDoiTuong.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboDoiTuong.SelectedValue == null)
            {
                cboDoiTuong.Text = "";
                cboDoiTuong.Focus();
            }
        }

        private void cboMaKho_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cboDoiTuong.SelectedItem != null)
                RefreshProductList();
        }

        private void RefreshProductList()
        {
            dgvHangHoa.DataSource = null;
            dgvHangHoa.AutoGenerateColumns = false;

            if (((Kho)cboMaKho.SelectedItem).Id != -1)
            {
                dgvHangHoa.DataSource = GetAllHangHoa();
            }
            else
            {
                List<HangHoa> lst = new List<HangHoa>();
                
                var list = (from d in GetAllCTKho()
                            where d.IdKho == ((Kho)cboMaKho.SelectedItem).Id
                            select d);

                if (list.ToList().Count > 0)
                {
                    foreach (var l in list)
                    {
                        HangHoa hanghoa = (from p in GetAllHangHoa()
                                           where p.Id == l.IdHangHoa
                                           select p).FirstOrDefault();

                        if (hanghoa.Id != 0)
                        {
                            lst.Add(hanghoa);
                        }
                    }
                    dgvHangHoa.DataSource = lst;
                }
            }
        }

        public List<HangHoa> GetAllHangHoa()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = hanghoaMgt.GetAllHangHoa();
            List<HangHoa> listHangThanhPham = (List<HangHoa>)resultObj.Result;

            return listHangThanhPham;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return listChiNhanhCongTy;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            List<TaiKhoanKeToan> listTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return listTaiKhoanKeToan;
        }

        public List<TheKho> GetAllTheKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = quanlykhoMgt.GetAllTheKho();
            List<TheKho> listTheKho = (List<TheKho>)resultObj.Result;

            return listTheKho;
        }

        public List<CTKho> GetAllCTKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = ctkhoMgt.GetAllCTKho();
            List<CTKho> listCTKho = (List<CTKho>)resultObj.Result;

            return listCTKho;
        }

        public List<Kho> GetAllKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khoMgt.GetAllKho();
            List<Kho> listKho = (List<Kho>)resultObj.Result;

            return listKho;
        }
    }
}
